package com.bangdream.michelia.view.activity.currency;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;

import com.bangdream.michelia.R;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.utils.SPUtils;
import com.bangdream.michelia.view.activity.base.BaseActivity;
import com.bangdream.michelia.view.fragment.currency.BackHandledFragment;
import com.bangdream.michelia.view.fragment.currency.WebViewFragment;

/**
 * Created by Mr.Wang on 2018/4/8.
 * 909732184@qq.com
 */

public class WebViewActivity extends BaseActivity implements BackHandledFragment.BackHandledInterface {
    private String stUrl;
    private BackHandledFragment mBackHandedFragment;
    private WebViewFragment webViewFragment;
    @Override
    public int getLayout() {
        return R.layout.activity_webview;
    }

    @Override
    public void findId() {

    }

    @Override
    public void setListener() {

    }

    @Override
    public void logic() {
        if(getIntent().getExtras()!=null){
            stUrl = getIntent().getStringExtra("url");
        }
        webViewFragment = new WebViewFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url", stUrl);
        webViewFragment.setArguments(bundle);
        addFragment(webViewFragment);
        showFragment(webViewFragment);
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }



    /** 添加Fragment **/
    public void addFragment(Fragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.show_layout, fragment);
        ft.commit();
    }

    /** 删除Fragment **/
    public void removeFragment(Fragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        // 设置Fragment的切换动画
        ft.setCustomAnimations(0, R.anim.base_slide_right_out);
        ft.remove(fragment);
        ft.commit();
    }

    /** 显示Fragment **/
    public void showFragment(Fragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        // 设置Fragment的切换动画
        ft.setCustomAnimations(R.anim.base_slide_right_in, R.anim.base_slide_remain);
        // 判断页面是否已经创建，如果已经创建，那么就隐藏掉
        ft.show(fragment);
        ft.commitAllowingStateLoss();
    }

    @Override
    public void setSelectedFragment(BackHandledFragment selectedFragment) {
        mBackHandedFragment = selectedFragment;
    }

    @Override
    public void onBackPressed() {
        if(mBackHandedFragment == null || !mBackHandedFragment.onBackPressed()){
            if(getSupportFragmentManager().getBackStackEntryCount() == 0){
                super.onBackPressed();
                //如果fragment没有消费掉返回键
                toNextActivity();
            }else{
                getSupportFragmentManager().popBackStack();
            }
        }
    }


    private void toNextActivity() {//根据是否保存有 token 判断去登录界面还是主界面
        Intent intent = null;
        String token = (String) SPUtils.get(WebViewActivity.this, "token", "");
        if (TextUtils.isEmpty(token)) {
            intent = new Intent(WebViewActivity.this, LoginActivity.class);
        } else {
            intent = new Intent(WebViewActivity.this, MainActivity.class);
        }
        startActivity(intent);
        finish();
    }
}
